package com.johnson.news;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import io.reactivex.Observable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.johnson.news.activity.WebViewActivity;
import com.johnson.news.glide.GlideApp;
import com.johnson.news.model.bean.LaunchPic;
import com.johnson.news.model.remote.RemoteDataSource;
import com.johnson.news.util.Utils;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.youmbe.library.BaseActivity;
import com.youmbe.library.BaseBindingActivity;
import com.youmbe.library.data.BaseDataWithBean;

import java.util.concurrent.TimeUnit;

public class SplashActivity extends BaseBindingActivity {
    ImageView splash_bg;
    TextView splash_copyright, loading_txt;
    LinearLayout splash_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.transparent));
        }

//        setContentView(R.layout.activity_splash);

        initViews();
//        RxPermissions rxPermissions = new RxPermissions(this);
//        rxPermissions.request(Manifest.permission.INTERNET)
//                .subscribe(new Consumer<Boolean>() {
//                    @Override
//                    public void accept(Boolean aBoolean) throws Exception {
//                        if(aBoolean) {
//                            getData();
//                        } else {
//                            finish();
//                        }
//                    }
//                });

        getData();
    }

    @Override
    protected void init() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_splash;
    }

    void initViews() {
        splash_bg = findViewById(R.id.splash_bg);
        splash_copyright = findViewById(R.id.splash_copyright);
        loading_txt = findViewById(R.id.loading_txt);
        splash_content = findViewById(R.id.splash_content);

        splash_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Object object = view.getTag();
                if(object != null && !TextUtils.isEmpty(object.toString())) {
                    openMain();

                    Intent intent = new Intent(activityInstance, WebViewActivity.class);
                    intent.putExtra(WebViewActivity.intent_key_url, object.toString());
                    startActivity(intent);
                }
            }
        });
    }

    void getData() {
        RemoteDataSource.getRemoteDataSource()
                .getSplashInfo()
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        loading_txt.setVisibility(View.INVISIBLE);
                        gotoMain();
                    }
                })
                .subscribe(new Consumer<LaunchPic>() {
                    @Override
                    public void accept(LaunchPic launchPic) throws Exception {
//                        launchPic.setEndtime("1587785359");//todo 测试代码
                        long currentTime = System.currentTimeMillis() / 1000;
                        if (currentTime >= Utils.parseLong(launchPic.getBegintime())
                                && currentTime <= Long.parseLong(launchPic.getEndtime())) {

                            GlideApp.with(activityInstance)
                                    .asBitmap()
                                    .load(launchPic.getPics())
                                    .into(new SimpleTarget<Bitmap>() {
                                        @Override
                                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                            if(resource != null) {
                                                splash_content.setVisibility(View.GONE);
//                                                splash_copyright.setVisibility(View.GONE);
                                                splash_bg.setImageBitmap(resource);
                                            }
                                        }
                                    });

                            splash_bg.setTag(launchPic.getLink());
                        }

//                        splash_copyright.setText(launchPic.getTitle());//修改：不受任何逻辑控制，文字也写死

//                        if(Utils.parseInt(launchPic.getStatus()) == 1) {//不显示版权和logo----改为没背背图时显示，不再受status控制
//                            splash_content.setVisibility(View.GONE);
//                            splash_copyright.setVisibility(View.GONE);
//                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Toast.makeText(activityInstance, getString(R.string.loading_failed_txt), Toast.LENGTH_SHORT).show();
                    }
                });
    }

    boolean hasOpenMain = false;
    void openMain() {
        synchronized (this) {
            if(!hasOpenMain) {
                hasOpenMain = true;
                Bundle bundle = new Bundle();
                startActivity(MainActivity.class, bundle);
                finish();
            }
        }
    }

    void gotoMain() {
        Observable.timer(2000, TimeUnit.MILLISECONDS)
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        openMain();
                    }
                });
    }
}
